package com.example.shouybl.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.example.shouybl.BaseApp;
import com.example.shouybl.R;
import com.example.shouybl.adapter.ActivityCommentAdapter;
import com.example.shouybl.bean.ActivityComment;
import com.example.shouybl.bean.ActivityDetBase;
import com.example.shouybl.bean.ActivityDetInfo;
import com.example.shouybl.bean.AppBaseBean;
import com.example.shouybl.constant.Path;
import com.example.shouybl.utils.ImageLoaderUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

public class ActionDetailActivity extends AppCompatActivity {

    private com.handmark.pulltorefresh.library.PullToRefreshListView ptractivitydetail;
    private ListView lvactivitydetail;
    private AsyncHttpClient client = new AsyncHttpClient();
    //评论
    private ArrayList<ActivityComment> comments = new ArrayList<>();
    private ActivityCommentAdapter commentAdapter;
    private RequestParams comment;
    private int page = 1;
    //活动详情
    private View view;
    private RequestParams activity;
    private ActivityDetInfo detInfo=new ActivityDetInfo();
    private TextView tvAname;
    private ImageView ivAct;
    private TextView tvWriter;
    private TextView tvPubdata;
    private TextView tvContent;
    private ImageView ivExpimg;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_detail);

        //活动详情
        addHead();
        //评论
        initView();
        initData();
        ptractivitydetail.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                initData();
            }
        });
    }
    //活动相关数据
    private void addHead() {
        view = View.inflate(getApplicationContext(), R.layout.item_add_actdetail_layout, null);
        initHeadView();
        client.post(Path.PATH_ACTIVITY_DETAIL, getActivity(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                TypeReference<ActivityDetBase> typeReference = new TypeReference<ActivityDetBase>() {
                };
                ActivityDetBase detBase = JSONObject.parseObject(responseString, ActivityDetBase.class);
                if (detBase != null && detBase.getState().equals("success")) {
                    detInfo = detBase.getInfo();
                }
                tvAname.setText(detInfo.getAname());
                tvWriter.setText(detInfo.getWriter());
                tvPubdata.setText(detInfo.getPubdate());
                tvContent.setText(detInfo.getContent());
                if (detInfo.getExpimg() != null) {
                    ImageLoader.getInstance().displayImage(detInfo.getExpimg(), ivExpimg, ImageLoaderUtil.getDefaultOptions());
                }
            }
        });

    }

    private void initHeadView() {
        tvAname = (TextView) view.findViewById(R.id.tv_actDetail_aname);
        ivAct = (ImageView) view.findViewById(R.id.iv_act);
        tvWriter = (TextView) view.findViewById(R.id.tv_actDetail_writer);
        tvPubdata = (TextView) view.findViewById(R.id.tv_actDetail_pubdate);
        tvContent = (TextView) view.findViewById(R.id.tv_actDetail_content);
        ivExpimg = (ImageView) view.findViewById(R.id.iv_actDetail_expimg);
    }
    public RequestParams getActivity() {
        activity=new RequestParams();
        activity.add("id", getIntent().getStringExtra("id"));
        activity.add("compare","3b37329163d1e64c814d3798d2400c02");
        return activity;
    }

    //评论相关数据
    private void initData() {

        client.post(Path.PATH_ACTIVITY_COMMENT, getComment(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                TypeReference<AppBaseBean<ActivityComment>> typeReference = new TypeReference<AppBaseBean<ActivityComment>>() {
                };
                AppBaseBean baseBean = JSONObject.parseObject(responseString, typeReference);
                if (baseBean != null && baseBean.getState().equals("success")) {
                    comments.addAll(baseBean.getInfo());
                }
                commentAdapter.notifyDataSetChanged();
            }
        });
    }

    private void initView() {
        this.ptractivitydetail = (PullToRefreshListView) findViewById(R.id.ptr_activity_detail);
        ptractivitydetail.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        lvactivitydetail=ptractivitydetail.getRefreshableView();
        commentAdapter = new ActivityCommentAdapter(comments, BaseApp.getInstance());
        lvactivitydetail.addHeaderView(view);
        lvactivitydetail.setAdapter(commentAdapter);
    }
    public RequestParams getComment() {
        comment = new RequestParams();
        comment.add("id", getIntent().getStringExtra("id"));
        comment.add("compare", "3410e3fb1be602a8d612144a412e794c");
        comment.add("page", String.valueOf(page));
        return comment;
    }
}
